package cn.tta.service.impl;

import cn.tta.common.constant.MessageConstant;
import cn.tta.common.constant.PasswordConstant;
import cn.tta.common.constant.StatusConstant;
import cn.tta.common.context.BaseContext;
import cn.tta.common.exception.AccountLockedException;
import cn.tta.common.exception.AccountNotFoundException;
import cn.tta.common.exception.PasswordErrorException;
import cn.tta.common.result.PageResult;
import cn.tta.mapper.EmployeeMapper;
import cn.tta.pojo.dto.EmployeeDTO;
import cn.tta.pojo.dto.EmployeeLoginDTO;
import cn.tta.pojo.dto.EmployeePageQueryDTO;
import cn.tta.pojo.entity.Employee;
import cn.tta.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        // 根据用户名查询数据库中的数据
        Employee employee = employeeMapper.getByUsername(employeeLoginDTO.getUsername());

        // 处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (employee == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        // md5加密
        String mdPassword = DigestUtils.md5DigestAsHex(employeeLoginDTO.getPassword().getBytes());
        // 密码对比
        if (!mdPassword.equals(employee.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        // 账号锁定
        if (Objects.equals(employee.getStatus(), StatusConstant.DISABLE)) {
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        return employee;
    }

    @Override
    public void save(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();

        // 对象拷贝属性
        BeanUtils.copyProperties(employeeDTO, employee);
        // 账号设置状态 1正常 0锁定
        employee.setStatus(StatusConstant.ENABLE);
        // 设置默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
        // 设置当前记录的创建时间和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        // 设置当前记录创建人id和修改人id
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());
        employeeMapper.insert(employee);
    }

    @Override
    public PageResult page(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        long total = page.getTotal();
        List<Employee> record = page.getResult();
        return new PageResult(total, record);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Employee build = Employee.builder()
                .id(id).status(status).build();
        employeeMapper.startOrStop(build);
    }

    @Override
    public Employee getById(Long id) {
        Employee employee = employeeMapper.getById(id);
        employee.setPassword("******");
        return employee;
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setUpdateUser(BaseContext.getCurrentId());
        employee.setUpdateTime(LocalDateTime.now());
        employeeMapper.startOrStop(employee);
    }


}
